<template>
  <cml-module-item :title="$t('cmlpanel.prompts')"
    :titleMore="$t('cmlpanel.prompts')"
    v-bind="$attrs"
    v-on="$listeners">
    <a-switch size="small"
      :disabled="!isReady"
      :checked="isStart"
      @change="aiVoiceFeedbackChange" />
    <template v-slot:detail>
      <voice-feedback-more ref="voiceFeedbackMore" :voiceNameOption="voiceNameArr" :voiceNameDefault="voiceName"></voice-feedback-more>
    </template>
  </cml-module-item>
</template>
<script>
import { Switch } from 'ant-design-vue'
import CmlModuleItem from '../../cml-module-item'
import VoiceFeedbackMore from './morePanel'
import { voiceFeedbackServer as vfs } from 'anov-core'

export default {
  name: 'VoiceFeedback',
  inheritAttrs: false,
  components: {
    ASwitch: Switch,
    CmlModuleItem,
    VoiceFeedbackMore
  },
  model: {},
  props: {},
  data() {
    return {
      isReady: false,
      isInit: true,
      voiceNameArr: [
        {
          label: '基础男声',
          value: 'x2_qige'
        },
        {
          label: '基础女声',
          value: 'x2_xiaoyuan'
        }
      ],
      voiceName: ''
    }
  },
  computed: {
    ...vfs.mapGetters(['isStart', 'synthesisType', 'thirdConfig']),
    titleText() {
      return this.$t('cmlpanel.prompts')
    },
    titleMore() {
      return this.$t('cmlpanel.prompts')
    }
  },
  watch: {},
  created() {
    this.isReady = vfs.isReady
  },
  mounted() {
    app.envServer.addEventListener('onLine', () => {
      (this.synthesisType == 'xf') && this.testVoiceServer(true)
    })
    app.envServer.addEventListener('offLine', () => {
      (this.synthesisType == 'xf') && this.testVoiceServer(false)
    })

    vfs.addEventListener('testStart', () => {
      if(this.isInit){
        this.startTip()
      }else {
        this.$refs.voiceFeedbackMore.voiceSuccessHandle()
      }
    })
    vfs.addEventListener('testError', err => {

      let msg = '语音提示接口智灵不可用，已为您切换为默认'
      app.msg({type: 'error', content: msg})
      vfs.changeSynthesisType('default')
      vfs.speak(msg,{volume:1})
    })

    if (this.thirdConfig.voiceName) {
      let arr = this.voiceNameArr
      for(let item of arr){
        if(item.value == this.thirdConfig.voiceName) return
      }
      this.voiceNameArr.push({
        label: '自选发音人',
        value: this.thirdConfig.voiceName
      })
      this.voiceName = this.thirdConfig.voiceName
    } else {
      this.voiceName = this.voiceNameArr[0].value
    }
  },
  beforeDestroy() {},
  methods: {
    aiVoiceFeedbackChange(e) {
      vfs.toggleOpen(e)
      if (this.synthesisType == 'xf') {
        vfs.synthesisType = 'xf'
        let flag = app.envServer.localNetwork.connected
        e && this.testVoiceServer(flag)
      } else {
        this.startTip(e)
      }
    },
    testVoiceServer(flag){
        if(!flag){
          setTimeout(() => {
            let msg = '网络连接断开，语音提示接口已切换到默认'
            app.msg({type: 'error', content: msg})
            vfs.changeSynthesisType('default')
            vfs.speak(msg)
          }, 100)
        }else {
          setTimeout(() => {
            vfs.test("测试", {volume: 0, voiceName: this.voiceName})
          }, 100)
        }
    },
    startTip(){
      if(this.synthesisType == 'xf'){vfs.setThirdConfig({ voiceName: this.voiceName })}
      let msg = this.titleText + (this.isStart ? this.$t('cmlpanel.start') : this.$t('cmlpanel.stop'))
      vfs.speak(msg, {volume: 1})
      app.msg({type: 'info', content: msg})
      this.isInit = false
    }
  }
}
</script>
<style lang="scss" scoped></style>
